//
//  Team9DesignCell.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/14.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9DesignCell.h"
#import "Team9BaseLabel.h"
#import "Team9BaseView.h"
#import "Team9DesignModel.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>

@interface Team9DesignCell ()

@property(nonatomic, strong)Team9BaseView *myBackView;
@property(nonatomic, strong)UIImageView *coverImageView;
@property(nonatomic, strong)UIImageView *faceImageView;
@property(nonatomic, strong)Team9BaseLabel *titleLabel;
@property(nonatomic, strong)Team9BaseLabel *title_subLabel;
@property(nonatomic, strong)UIImageView *likeImageView;
@property(nonatomic, strong)Team9BaseLabel *likeLabel;
// 独有部分
@property(nonatomic, strong)UIView *work_design_priceView;
@property(nonatomic, strong)UILabel *work_design_priceLabel;
@end

@implementation Team9DesignCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)createView{
    
    self.myBackView = [[Team9BaseView alloc] init];
    [self.contentView addSubview:self.myBackView];
    
    self.coverImageView = [UIImageView new];
    [self.myBackView addSubview:self.coverImageView];
    
    self.faceImageView = [UIImageView new];
    [self.contentView addSubview:self.faceImageView];
    
    self.likeImageView = [UIImageView new];
    [self.contentView addSubview:self.likeImageView];
    
    self.titleLabel = [[Team9BaseLabel alloc] initWith:LableColorTypeOrangeGreen];
    [self.contentView addSubview:self.titleLabel];
    
    self.title_subLabel = [[Team9BaseLabel alloc] init];
    [self.contentView addSubview:self.title_subLabel];
    
    self.likeLabel = [[Team9BaseLabel alloc] init];
    [self.contentView addSubview:self.likeLabel];
    
    self.work_design_priceLabel = [[Team9BaseLabel alloc] init];
    [self.contentView addSubview:self.work_design_priceLabel];
    
// 独有部分
    self.work_design_priceView = [UIView new];
    [self.contentView addSubview:self.work_design_priceView];
    
    self.work_design_priceLabel = [UILabel new];
    [self.contentView addSubview:self.work_design_priceLabel];
    
    
    [self p_contrains];
    
    self.contentView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
//    self.myBackView.backgroundColor = [UIColor whiteColor];
    self.myBackView.layer.cornerRadius = 5;
    self.myBackView.layer.masksToBounds = YES;
    self.faceImageView.layer.cornerRadius = 20 * WIDTH /375;
    self.faceImageView.layer.masksToBounds = YES;
    self.likeImageView.image = [UIImage imageNamed:@"like"];
    self.title_subLabel.font = [UIFont systemFontOfSize:13 * WIDTH / 375];
    self.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTH / 375];
    self.likeLabel.font = [UIFont systemFontOfSize:10 * WIDTH / 375];
    self.likeLabel.textAlignment = NSTextAlignmentCenter;
// 独有部分
    self.work_design_priceLabel.font = [UIFont systemFontOfSize:15 * WIDTH / 375];
    self.work_design_priceLabel.textColor = [UIColor whiteColor];
    self.work_design_priceLabel.textAlignment = NSTextAlignmentCenter;
    self.work_design_priceView.backgroundColor = [UIColor orangeColor];
    self.work_design_priceView.alpha = 0.5;
//    [self test];
}
- (void)test{
    self.coverImageView.image = [UIImage imageNamed:@"test"];
    self.faceImageView.backgroundColor = [UIColor redColor];
    self.titleLabel.text = @"15042341680";
    self.title_subLabel.text= @"15042341680";
    self.titleLabel.backgroundColor = [UIColor cyanColor];
    self.title_subLabel.backgroundColor = [UIColor cyanColor];
    self.likeLabel.text = @"32";
}
- (void)p_contrains{
    [self.myBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
    
    [self.coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.equalTo(self.myBackView);
        make.height.equalTo(self.coverImageView.mas_width).multipliedBy(0.6);
    }];
    
    [self.faceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.coverImageView.mas_bottom).offset(10);
        make.left.equalTo(self.myBackView).offset(10);
        make.height.equalTo(@(40 * WIDTH / 375));
        make.width.equalTo(self.faceImageView.mas_height);
    }];
    
    [self.likeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.faceImageView);
        make.height.width.equalTo(self.faceImageView).multipliedBy(0.5);
        make.right.equalTo(self.myBackView).offset(-10);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.faceImageView);
        make.left.equalTo(self.faceImageView.mas_right).offset(10);
        make.right.equalTo(self.likeImageView.mas_left).offset(-10);
    }];
    
    [self.title_subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.faceImageView);
        make.left.equalTo(self.faceImageView.mas_right).offset(10);
        make.right.equalTo(self.likeImageView.mas_left).offset(-10);
    }];
    
    [self.likeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.equalTo(self.likeImageView);
        make.bottom.equalTo(self.faceImageView);
    }];
//独有部分
    [self.work_design_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.myBackView).offset(10);
        make.width.equalTo(self).multipliedBy(0.2);
        make.height.equalTo(@(30 * WIDTH / 375));
        make.right.equalTo(self.myBackView);
    }];
    [self.work_design_priceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.work_design_priceLabel);
    }];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.faceImageView sd_setImageWithURL:[NSURL URLWithString:self.model.author_face] placeholderImage:[UIImage imageNamed:@"333"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        [UIView animateWithDuration:0.3 animations:^{
            CATransition *transition = [CATransition animation];
            transition.type = @"fade";
            [transition setDuration:0.5];
            [transition setRepeatCount:1];
            // 向imageView上添加动画效果
            [self.faceImageView.layer addAnimation:transition forKey:@"transition"];
        }];
        
    }];
    
    [Team9Tool sd_progress_imageView:self.coverImageView image:self.model.cover_pic placeholderImage:@"111"];
    
//    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:self.model.cover_pic] placeholderImage:[UIImage imageNamed:@"111"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//        [UIView animateWithDuration:0.3 animations:^{
//            CATransition *transition = [CATransition animation];
//            transition.type = @"fade";
//            [transition setDuration:0.5];
//            [transition setRepeatCount:1];
//            // 向imageView上添加动画效果
//            [self.coverImageView.layer addAnimation:transition forKey:@"transition"];
//        }];
//    }];
    
    self.titleLabel.text = self.model.title;
    self.title_subLabel.text = self.model.title_sub;
    self.likeLabel.text = self.model.liked_count;
    
    self.work_design_priceLabel.text = [NSString stringWithFormat:@"%@/平米", self.model.work_design_price];
    
    if (self.model.work_design_price.integerValue) {
        [self.work_design_priceLabel setHidden:NO];
        [self.work_design_priceView setHidden:NO];
    } else {
        [self.work_design_priceLabel setHidden:YES];
        [self.work_design_priceView setHidden:YES];
    }
}

@end
